package com.yt.server.db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.yt.server.api.db.BatchPreparedStatementSetter;
import com.yt.server.api.db.ICallableStatementCreator;
import com.yt.server.api.db.IDBSupport;
import com.yt.server.api.db.IPreparedStatementCreator;
import com.yt.server.api.db.ISqlProvider;
import com.yt.server.db.manage.DBManager;

/**
 * 数据库支持(类似JdbcTemplate)
 * @version 2013.11.7 一版
 */
public class DBSupport implements IDBSupport {

	private static DBSupport instance = null;

	public static DBSupport getInstance() {
		if (instance == null)
			instance = new DBSupport();
		return instance;
	}

	/**
	 * 初始化
	 * 
	 * @param path
	 */
	public void init(String path) {
		DBManager.getInstance().init(path);
	}

	public void execute(String sql) {
	}

	public void query(String jndi, String sql) {
	}

	public void update(String jndi, String sql) {
	}

	public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) {
		return null;
	}

	/**
	 * 创建PreparedStatement
	 */
	private static class PreparedStatementCreator implements
			IPreparedStatementCreator, ISqlProvider {

		private final String sql;

		public PreparedStatementCreator(String sql) {
			this.sql = sql;
		}

		@Override
		public PreparedStatement createPreparedStatement(Connection con)
				throws SQLException {
			return con.prepareStatement(this.sql);
		}

		@Override
		public String getSql() {
			return this.sql;
		}
	}

	/**
	 * 创建CallableStatement
	 */
	private static class CallableStatementCreator implements
			ICallableStatementCreator, ISqlProvider {

		private final String callString;

		public CallableStatementCreator(String callString) {
			this.callString = callString;
		}

		@Override
		public CallableStatement createCallableStatement(Connection con)
				throws SQLException {
			return con.prepareCall(this.callString);
		}

		@Override
		public String getSql() {
			return this.callString;
		}
	}
}